package fi.foyt.cs.api.json;

import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import fi.foyt.cs.controller.SceneController;
import fi.foyt.cs.persistence.domainmodel.scene.Scene;
import fi.foyt.cs.persistence.domainmodel.scene.SceneCategory;

public class SceneRenderer implements EntityRenderer<Scene> {

  @Override
  public JSONObject getAsJson(Locale locale, Scene scene) {
    JSONObject jsonObject = new JSONObject();

    SceneController sceneController = new SceneController();
    
    try {
      jsonObject.put("id", scene.getId().getId());

      if (scene.getCategoryId() != null) {
        SceneCategory sceneCategory = sceneController.findSceneCategory(scene.getCategoryId());
        if (sceneCategory != null) {
          jsonObject.put("category", EntityRendererVault.getInstance().renderEntity(locale, sceneCategory));
        }
      }
      
    } catch (JSONException e1) {
      throw new RenderingException(e1);
    }

    return jsonObject;
  }

}
